package Entity.EntityMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import Entity.Book;
import Entity.BookInvoice;
import Entity.Invoice;
import Entity.User;

public class EntityMapper {
	public ArrayList<User> getUserResults(ResultSet results) {
		try {
			ArrayList<User> result = new ArrayList<User>();
        	if(results != null){
				while (results.next()) {
					User user = new User();
					user.setId(results.getInt("id"));
					user.setUsername(results.getString("username"));
					user.setPassword(results.getString("password"));
					user.setSalt(results.getString("salt"));
					user.setEmail(results.getString("email"));
					user.setRole(results.getString("role"));
					user.setTelephone(results.getInt("telephone"));
					user.setLoyaltyPoint(results.getInt("loyalty_Point"));
	
					result.add(user);
				}
				return result;
        	}
		} catch (SQLException ex) {
			Logger.getLogger(EntityMapper.class.getName()).log(Level.SEVERE,
					null, ex);
		}
		return null;
	}

	public ArrayList<Book> getBookResults(ResultSet results) {
		try {
			ArrayList<Book> result = new ArrayList<Book>();
			if (results != null) {
				while (results.next()) {
					Book book = new Book();
					book.setId(results.getInt("id"));
					book.setTitle(results.getString("title"));
					book.setDescription(results.getString("description"));
					book.setPrice(results.getFloat("price"));
					book.setQuantity(results.getInt("quantity"));
					book.setPublisher(results.getString("publisher"));
					book.setLanguage(results.getString("language"));
					book.setNoOfView(results.getInt("no_of_viewed"));
					book.setNoOfPurchase(results.getInt("no_of_purchased"));

					result.add(book);
				}
				return result;
			}
		} catch (SQLException ex) {
			Logger.getLogger(EntityMapper.class.getName()).log(Level.SEVERE,
					null, ex);
		}
		return null;
	}

	public ArrayList<Invoice> getInvoiceResults(ResultSet results) {
		try {
			ArrayList<Invoice> result = new ArrayList<Invoice>();
			if (results != null) {
				while (results.next()) {
					Invoice invoice = new Invoice();
					invoice.setId(results.getInt("id"));
					invoice.getUser().setId(results.getInt("user_id"));
					invoice.setDate(results.getDate("timestamp"));
					invoice.setRefund(results.getBoolean("refund_request"));
					invoice.setIsRefunded(results.getBoolean("is_refunded"));
					invoice.setRemarks(results.getString("remarks"));
					invoice.setAttn(results.getString("attn"));
					invoice.setAddress(results.getString("address"));
					invoice.setLoyaltyPointUsed(results.getBoolean("loyalty_point_used"));

					result.add(invoice);
				}
				return result;
			}
		} catch (SQLException ex) {
			Logger.getLogger(EntityMapper.class.getName()).log(Level.SEVERE,
					null, ex);
		}
		return null;
	}

	public ArrayList<BookInvoice> getBookInvoiceResults(ResultSet results) {
		try {
			ArrayList<BookInvoice> result = new ArrayList<BookInvoice>();
			if (results != null) {
				while (results.next()) {
					BookInvoice bookInvoice = new BookInvoice();
					bookInvoice.getBook().setId(results.getInt("book_id"));
					bookInvoice.setInvoiceID(results.getInt("invoice_id"));
					bookInvoice.setQuantity(results.getInt("quantity"));

					result.add(bookInvoice);
				}
				return result;
			}
		} catch (SQLException ex) {
			Logger.getLogger(EntityMapper.class.getName()).log(Level.SEVERE,
					null, ex);
		}
		return null;
	}
}
